package yw

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func sales(r *gin.RouterGroup) {
	sales := r.Group("/sales")
	{
		sales.POST(sql.RequestAdd, salesAdd)
		sales.POST(sql.RequestUpdate, salesUpdate)
		sales.GET(sql.RequestDelete, salesDelete)
		sales.GET(sql.RequestList, salesList)
		//sales.GET(sql.RequestData, salesData)
	}
}

func salesAdd(c *gin.Context) {
	sql.HttpHandle(sql.ADD, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var bean model.ProductSales
		if err := c.Bind(&bean); err != nil {
			fmt.Println(err.Error())
			db.Error = errors.New(sql.ErrorParams)
			return db, nil
		}
		if err := db.Transaction(func(tx *gorm.DB) error {
			if err := tx.Table("yw_product").
				Where("yw_product.id = ?", bean.ProductId).
				Updates(map[string]interface{}{
					"inventory_number": gorm.Expr("inventory_number - ?", bean.SalesNumber),
					"inventory_weight": gorm.Expr("inventory_weight - ?", bean.SalesWeight),
				}).Error; err != nil {
				return err
			}
			if err := tx.Table("yw_product_sales").Create(&bean).Error; err != nil {
				return err
			}
			return nil
		}); err != nil {
			db.Error = err
			return db, nil
		}
		return db, bean
	})
}

func salesUpdate(c *gin.Context) {
	sql.HttpHandle(sql.ADD, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var bean model.ProductSales
		if err := c.Bind(&bean); err != nil {
			fmt.Println(err.Error())
			db.Error = errors.New(sql.ErrorParams)
			return db, nil
		}
		if err := db.Transaction(func(tx *gorm.DB) error {
			var productSales model.ProductSales
			if err := tx.Table("yw_product_sales").
				Where("id = ?", bean.ID).
				Find(&productSales).Error; err != nil {
				return err
			}

			var number = productSales.SalesNumber - bean.SalesNumber
			var weight = productSales.SalesWeight - bean.SalesWeight
			if err := tx.Table("yw_product").
				Where("yw_product.id = ?", bean.ProductId).
				Updates(map[string]interface{}{
					"inventory_number": gorm.Expr("inventory_number - ?", number),
					"inventory_weight": gorm.Expr("inventory_weight - ?", weight),
				}).Error; err != nil {
				return err
			}
			if err := tx.Table("yw_product_sales").Updates(&bean).Error; err != nil {
				return err
			}
			return nil
		}); err != nil {
			db.Error = err
			return db, nil
		}
		return db, bean
	})
}

func salesList(c *gin.Context) {
	sql.HttpHandle(sql.GET, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var productSales []model.ProductSales
		var productId = c.Query("productId")
		if productId != "" {
			db = db.Where("product_id = ?", productId)
		}

		db.Table("yw_product_sales").
			Select("yw_product_sales.*,yw_product.product_brand,yw_product.product_name,yw_product.specification").
			Joins("left join yw_product ON yw_product.id = yw_product_sales.product_id").
			Find(&productSales)

		return db, productSales
	})
}

func salesDelete(c *gin.Context) {
	sql.HttpHandle(sql.DELETE, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var tableName = "yw_product_sales"
		var id = c.Query("id")
		var productSalesBean model.ProductSales
		if err := db.Transaction(func(tx *gorm.DB) error {
			//查询销售激励
			if err := tx.Table(tableName).
				Where("id = ?", id).
				Find(&productSalesBean).Error; err != nil {
				return err
			}
			//修改库存
			if err := tx.Table("yw_product").
				Where("yw_product.id = ?", productSalesBean.ProductId).
				Updates(map[string]interface{}{
					"inventory_number": gorm.Expr("inventory_number + ?", productSalesBean.SalesNumber),
					"inventory_weight": gorm.Expr("inventory_weight + ?", productSalesBean.SalesWeight),
				}).Error; err != nil {
				return err
			}
			//删除销售记录
			if err := tx.Table(tableName).Where("id = ?", id).Delete(&productSalesBean).Error; err != nil {
				return err
			}
			return nil
		}); err != nil {
			db.Error = err
			return db, nil
		}
		return db, nil
	})

}
